<?php

/**
 * Created by PhpStorm.
 * User: zhoujinda
 * Date: 2015/12/23
 * Time: 16:03
 */
namespace Admin\Controller;

use Admin\Api\MessageApi;
use Admin\Api\MessageBoxApi;

class MessageController extends AdminController{

    protected function _initialize(){
        parent::_initialize();
        $this->assignTitle('消息管理');
    }

    public function index(){
        //get.startdatetime
        $startdatetime = I('startdatetime',date('Y/m/d H:i',time()-24*3600*30),'urldecode');
        $enddatetime = I('enddatetime',date('Y/m/d H:i',time()),'urldecode');
        $msg_type = I('msg_type','','urldecode');
        $this->assign('msg_type',$msg_type);

        $startdatetime = urldecode($startdatetime);
        $enddatetime = urldecode($enddatetime);

        $msg_type = urldecode($msg_type);

        if(IS_POST){
            $uid = I('post.uid',0,'int');
        }else{
            $uid = I('uid',0,'int');
        }


        //分页时带参数get参数
        $params = array(
            'startdatetime'=>$startdatetime,
            'enddatetime'=>$enddatetime,
            'msg_type'=>$msg_type
        );

        $startdatetime = strtotime($startdatetime);
        $enddatetime = strtotime($enddatetime);

        if($startdatetime === FALSE || $enddatetime === FALSE){
            LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
            $this->error(L('ERR_DATE_INVALID'));
        }

        $map = array();

        if(!empty($uid) && $uid!==-2){
            $map['to_id'] = $uid;
        }

        $map['send_time'] = array(array('EGT',$startdatetime),array('elt',$enddatetime),'and');
        $map['name'] = array('like',"%$msg_type%");

        $field = 'm.id,title,content,to_id,name,from_id,summary,send_time';
        $page = array('curpage' => I('get.p', 0), 'size' => 15);
        $order = " m.id desc ";
        //
        $result = apiCall(MessageApi::QUERY_MESSAGE,array($map,$page,$order,$params,$field));
        //
        if($result['status']){
            $this->assign('startdatetime',$startdatetime);
            $this->assign('enddatetime',$enddatetime);
            $this->assign('show',$result['info']['show']);
            $this->assign('list',$result['info']['list']);
            $this->display();
        }else{
            LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
            $this->error(L('UNKNOWN_ERR'));
        }
    }


    /**
     * 删除
     */
    public function delete(){

        $map = array('msg_id' => I('id', -1));
        $result = apiCall(MessageBoxApi::DELETE,array($map));

        if($result['status']){
            $map = array('id' => I('id', -1));
            $result = apiCall(MessageApi::DELETE,array($map));
            if($result['status']){
                $this -> success(L('RESULT_SUCCESS'));
            }else{
                LogRecord('[INFO]' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
                $this->error($result['info']);
            }
        }else{
            LogRecord('[INFO]' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
            $this->error($result['info']);
        }

    }

    /**
     * 批量删除
     */
    public function bulkDelete(){
        $ids = I('ids', -1);
        if ($ids === -1) {
            $this -> error(L('ERR_PARAMETERS'));
        }
        $ids = implode(',', $ids);
        $map = array('msg_id' => array('in', $ids));

        $result = apiCall(MessageBoxApi::DELETE,array($map));

        if($result['status']){
            $map = array('id' => array('in', $ids));

            $result = apiCall(MessageApi::DELETE,array($map));
            if($result['status']){
                $this -> success(L('RESULT_SUCCESS'));
            }else{
                LogRecord('[INFO]' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
                $this->error($result['info']);
            }
        }else{
            LogRecord('[INFO]' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
            $this->error($result['info']);
        }

    }

    /**
     * 推送消息
     */
    public function pushMessage(){

        $id = I('get.id',-1);
        if($id==-1){
            $this->error('参数缺失');
        }

        $result = apiCall(MessageApi::PUSH_MESSAGE_BY_MESSAGE_ID,array($id));
        if($result['status']){
            $this->success('推送成功！');
        }else{
            $this->error($result['info']);
        }

    }

}